package sk.gtug.pivoplus.malepivo;

import sk.m217.actionbarcompat.ActionBarActivity;
import sk.m217.actionbarcompat.ActionBarCompat;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;

public class PodnikDetailActivity extends ActionBarActivity implements OnRatingBarChangeListener {
	
	private String evalId;
	
	//private String evalTypeId;
	
	/** app controller instance */
	protected Application app;
	
	
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.podnikdetail);
        // setting up action bar
        getActionBarHelper().initActionBar();
        ActionBarCompat.setDisplayHomeAsUpEnabled(this, true);

        Gallery gallery = (Gallery) findViewById(R.id.photoGallery);
        gallery.setAdapter(new ImageAdapter(this));
        
        // reference rating
        RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar1);
        ratingBar.setOnRatingBarChangeListener(this);
        
        // retrieving intent arguments
        evalId = getIntent().getExtras().getString("evalId");
        //evalTypeId = getIntent().getExtras().getString("evalTypeId");
        
        // referencing application object used later to call services
        app = (Application) getApplication();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch (item.getItemId()) {
    		case android.R.id.home:
    	        startActivity(PubListActivity.createIntent(this));
    			break;
    	}
    	return super.onOptionsItemSelected(item);
    }

    // ------------------------------------------------------------------------
    //
    // Event handlers
    //
    // ------------------------------------------------------------------------
    
    /**
     * handle touch events
     * @param v
     */
    @Override
	public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
    	Float ratingValue = ratingBar.getRating();
    	app.evaluteServiceCall(evalId, "Pivo", ratingValue.toString(), "");
	}
    
    // ------------------------------------------------------------------------
    //
    // Adapter
    //
    // ------------------------------------------------------------------------
    
    public class ImageAdapter extends BaseAdapter
    {
    	int mGalleryItemBackground;
        private Context mContext;

        private Integer[] mImageIds = {
                R.drawable.sample_1,
                R.drawable.sample_2,
                R.drawable.sample_3,
                R.drawable.sample_4,
                R.drawable.sample_5,
                R.drawable.sample_6,
                R.drawable.sample_7
        };

        public ImageAdapter(Context c) {
            mContext = c;
            TypedArray attr = mContext.obtainStyledAttributes(R.styleable.HelloGallery);
            mGalleryItemBackground = attr.getResourceId(
                    R.styleable.HelloGallery_android_galleryItemBackground, 0);
            attr.recycle();
        }
    	
        public int getCount() {
            return mImageIds.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView = new ImageView(mContext);

            imageView.setImageResource(mImageIds[position]);
            imageView.setLayoutParams(new Gallery.LayoutParams(450, 300));
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setBackgroundResource(mGalleryItemBackground);

            return imageView;
        }
    	
    }

	
    
    
}
